<?php
class ControllerExtensionFeed extends Controller {
    public function index() {
        $this->language->load('extension/feed');

        $this->document->setTitle($this->language->get('heading_title')); 

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('heading_title'),
            'href'      => $this->url->link('extension/feed', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_no_results'] = $this->language->get('text_no_results');
        $this->data['text_confirm'] = $this->language->get('text_confirm');

        $this->data['column_name'] = $this->language->get('column_name');
        $this->data['column_status'] = $this->language->get('column_status');
        $this->data['column_action'] = $this->language->get('column_action');

        if (isset($this->session->data['success'])) {
            $this->data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $this->data['success'] = '';
        }

        if (isset($this->session->data['error'])) {
            $this->data['error'] = $this->session->data['error'];

            unset($this->session->data['error']);
        } else {
            $this->data['error'] = '';
        }

        $this->app->model('setting/extension', true, true);

        $extensions = $this->model_setting_extension->getInstalled('feed');

        foreach ($extensions as $key => $value) {
            if (!file_exists($this->load->get_path('dir_app') . 'controller/feed/' . $value . '.php')) {
                $this->model_setting_extension->uninstall('feed', $value);

                unset($extensions[$key]);
            }
        }

        $this->data['extensions'] = array();

        $files = glob($this->load->get_path('dir_app') . 'controller/feed/*.php');

        if ($files) {
            foreach ($files as $file) {
                $extension = basename($file, '.php');

                $this->language->load('feed/' . $extension);

                $action = array();

                if (!in_array($extension, $extensions)) {
                    $action[] = array(
                        'text' => $this->language->get('text_install'),
                        'href' => $this->url->link('extension/feed/install', 'token=' . $this->session->data['token'] . '&extension=' . $extension, 'SSL')
                    );
                } else {
                    $action[] = array(
                        'text' => $this->language->get('text_edit'),
                        'href' => $this->url->link('feed/' . $extension . '', 'token=' . $this->session->data['token'], 'SSL')
                    );

                    $action[] = array(
                        'text' => $this->language->get('text_uninstall'),
                        'href' => $this->url->link('extension/feed/uninstall', 'token=' . $this->session->data['token'] . '&extension=' . $extension, 'SSL')
                    );
                }

                $this->data['extensions'][] = array(
                    'name'   => $this->language->get('heading_title'),
                    'status' => $this->setting->get($extension . '_status') ? $this->language->get('text_enabled') : $this->language->get('text_disabled'),
                    'action' => $action
                );
            }
        }

        $this->template = 'extension/feed.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }

    public function install() {
        $this->language->load('extension/feed');

        if (!$this->auth->has_permission('modify', 'extension/feed')) {
            $this->session->data['error'] = $this->language->get('error_permission'); 

            $this->redirect($this->url->link('extension/feed', 'token=' . $this->session->data['token'], 'SSL'));
        } else {
            $this->app->model('setting/extension', true, true);

            $this->model_setting_extension->install('feed', $this->request->get['extension']);

            $this->app->model('user/user_group', true, true);

            $this->model_user_user_group->addPermission($this->user->get_id(), 'access', 'feed/' . $this->request->get['extension']);
            $this->model_user_user_group->addPermission($this->user->get_id(), 'modify', 'feed/' . $this->request->get['extension']);	

            $action = $this->app->action('feed/' .  $this->request->get['extension']);

            $controller = $this->app->controller($action);

            if (method_exists($controller, 'install')) {
                $class->install();
            }

            $this->redirect($this->url->link('extension/feed', 'token=' . $this->session->data['token'], 'SSL'));			
        }
    }

    public function uninstall() {
        $this->language->load('extension/feed');

        if (!$this->auth->has_permission('modify', 'extension/feed')) {
            $this->session->data['error'] = $this->language->get('error_permission'); 

            $this->redirect($this->url->link('extension/feed', 'token=' . $this->session->data['token'], 'SSL'));
        } else {		
            $this->app->model('setting/extension', true, true);
            $this->app->model('setting/setting', true, true);

            $this->model_setting_extension->uninstall('feed', $this->request->get['extension']);

            $this->model_setting_setting->deleteSetting($this->request->get['extension']);

            $action = $this->app->action('feed/' .  $this->request->get['extension']);

            $controller = $this->app->controller($action);

            if (method_exists($controller, 'uninstall')) {
                $class->uninstall();
            }

            $this->redirect($this->url->link('extension/feed', 'token=' . $this->session->data['token'], 'SSL'));
        }
    }
}
?>
